package com.yx.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * @Classname DateUtils
 * @Description TODO:日期格式化
 * @Date 2021/05/14 18:49
 * @Created by yx
 */
public class DateUtils {
    private static SimpleDateFormat[] simpleDateFormat = new SimpleDateFormat[5];

    static {
        simpleDateFormat[0] = new SimpleDateFormat("yyyy-MM-dd");
        simpleDateFormat[1] = new SimpleDateFormat("yyyy/MM/dd");
        simpleDateFormat[2] = new SimpleDateFormat("MM-dd-yyyy");
        simpleDateFormat[3] = new SimpleDateFormat("MM/dd/yyyy");
        simpleDateFormat[4] = new SimpleDateFormat("yyyy年MM月dd日");
    }

    /**
     * @param date 字符串格式日期
     * @return java.util.Date
     * @Author:yx
     * @Date 2021/04/22 19:24
     * @Description:字符串转Java.util.Date
     **/
    public static java.util.Date stringToDate(String date) {
        for (int i = 0; i < simpleDateFormat.length; i++) {
            try {
                return simpleDateFormat[i].parse(date);
            } catch (ParseException e) {
            }
        }
        return null;
    }

    /**
     * @param date java.util.Date格式日期
     * @return java.lang.String
     * @Author:yx
     * @Date 2021/04/22 19:27
     * @Description: 日期转字符串
     **/
    public static String dateToString(java.util.Date date) {
        //yyyy-MM-dd为数据库的date格式,本人习惯与数据库一致
        return simpleDateFormat[0].format(date);
    }

    /**
     * @param date java.util.Date格式日期
     * @return java.sql.Date
     * @Author:yx
     * @Date 2021/04/22 19:28
     * @Description: java.util.Date转java.sql.Date
     **/
    public static java.sql.Date dataToSql(java.util.Date date) {
        return new java.sql.Date(date.getTime());
    }
}
